package com.terabits.smartriver.service.impl;

import com.terabits.smartriver.mapper.*;
import com.terabits.smartriver.meta.po.*;
import com.terabits.smartriver.service.ProjectService;
import com.terabits.smartriver.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Yin on 2019/3/13
 */
@Service
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProjectServiceImpl extends BaseService implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private ProjectMaterialMapper projectMaterialMapper;

    @Autowired
    private ProjectTaskMapper projectTaskMapper;

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Resource
    private RegionMapper regionMapper;

    @Override
    public List<PageData> getProject(PageData pageData) {
        return projectMapper.selectProject(pageData);
    }

    @Override
    public int addProject(Project project) {
        List projects = findObjectsByProperty("projectNum", project.getProjectNum(), projectMapper, Project.class);
        if (projects != null && projects.size() != 0) {
            return -1;
        }

        Enterprise enterprise = enterpriseMapper.selectByPrimaryKey(project.getEnterpriseId());
        project.setProvinceId(enterprise.getProvinceId());
        project.setCityId(enterprise.getCityId());
        project.setDistrictId(enterprise.getDistrictId());
        project.setStreetId(enterprise.getStreetId());
        project.setTown(enterprise.getTown());

        return projectMapper.insertSelective(project);
    }

    @Override
    public int updateProject(Project project) {
        return projectMapper.updateByPrimaryKeySelective(project);
    }

    @Override
    public int deleteProject(Integer id) {
        String projectNum = projectMapper.selectByPrimaryKey(id).getProjectNum();
        String boxSQL = " SELECT * FROM box WHERE project_num ='" + projectNum + "'";
        List<PageData> box = regionMapper.selectListSQL(boxSQL);

        if (box == null || box.size() == 0) {
            return projectMapper.deleteByPrimaryKey(id);
        }
        return -1;
    }

    @Override
    public List<ProjectMaterial> getProjectMaterial(ProjectMaterial projectMaterial) {
        return projectMaterialMapper.select(projectMaterial);
    }

    @Override
    public Integer deleteProjectMaterial(ProjectMaterial projectMaterial) {
        return projectMaterialMapper.delete(projectMaterial);
    }

    @Override
    public int addProjectMaterial(ProjectMaterial projectMaterial) {
        return projectMaterialMapper.insertSelective(projectMaterial);
    }

    @Override
    public List<PageData> getProjectSchedule(PageData pageData) {
        return projectMapper.selectProjectSchedule(pageData);
    }

    @Override
    public List<PageData> getProjectTask(PageData pageData) {
        return projectMapper.selectProjectTask(pageData);
    }

    @Override
    public int addProjectTask(ProjectTask projectTask) {
        return projectTaskMapper.insertSelective(projectTask);
    }

    @Override
    public int updateProjectTask(ProjectTask projectTask) {
        return projectTaskMapper.updateByPrimaryKeySelective(projectTask);
    }

    @Override
    public List<PageData> getProjectAcceptance(PageData pageData) {
        return projectMapper.selectProjectAcceptance(pageData);
    }

}
